<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Cache\Driver\Redis;

class MovementController extends AdminbaseController{
	protected $user_merssage_model;
	protected $area_city;
	
	function _initialize() {
		parent::_initialize();
		$this->user_merssage_model = D("Common/UserMerssage");
		$this->area_city = D("Common/AreaCity");
	}
//	//推送 消息
//	function push_all(){
//		//查找 数据库中 发送中的 消息
//		$push_one = $this->user_merssage_model
//			->where("send_status='1' and status = 1")
//			->order("create_date desc ")
//			->limit(1)
//			->find();
//
//		$app_key = 'bb15edb8b0a59a3e172b4ea0';
//		$master_secret = 'ca206e50e6a37e0174dec8c2';
//		$registration_id = '171976fa8a8974a6b0d';
//
//		$client = new JPush($app_key, $master_secret);
//
//		try {
//			$push_payload = $client->push()
////				->setPlatform('all')
////				->addAllAudience()
//				->setPlatform(array('ios', 'android'))
//				->addRegistrationId($registration_id)
//				->setNotificationAlert($push_one['content'])
//				->send();
//
//			//更改 推送状态 为发送成功
//			$data['status'] = 2;
//			$this->user_merssage_model->where("id = ".$push_one['id'])->data($data)->save();
//		}catch (APIConnectionException $e){
//			//更改 推送状态 为发送失败
//			$data['status'] = 3;
//			$this->user_merssage_model->where("id = ".$push_one['id'])->data($data)->save();
//		}
//	}
	
	//消息列表
	function lists(){
		$p    = $this->user_merssage_model->getpage(10);
		$where_and = '';
		$sea_title = I('title');
		$sea_start_time = I('start_time');
		$sea_end_time = I('end_time');
		$sea_send_status = I("status");
		if($sea_title){
			$where_and .= " and title like '%".$sea_title."%'";
			$this->assign('sea_title',$sea_title);
		}
		if($sea_start_time){
			$where_and .= " and send_date > '".$sea_start_time."'";
			$this->assign('sea_start_time',$sea_start_time);
		}
		if($sea_end_time){
			$where_and .= " and send_date < '".$sea_end_time."'";
			$this->assign('sea_end_time',$sea_end_time);
		}
		if($sea_send_status){
			$where_and .= " and status = ".$sea_send_status;
			$this->assign('sea_send_status',$sea_send_status);
		}


		$list = $this->user_merssage_model
			->where("send_status='1'".$where_and)
			->order("create_date desc")
			->select();


		$this->list = $list;
		$page = $p->show();
		$this->assign('data',$list);
		$this->assign('page',$page);
		$this->display();
	}

	//搜索
	function serachlists(){
		$title=I('param.title', 0);    //标题
		$time=I('param.time', 0);        //时间
		$type=I('param.type', 0);			//范围
		$status=I('param.status', 0);		 //状态
		$data=$this->user_merssage_model->serach($title,$time,$type,$status);
		$value['title']=$title;
		$value['time']=$time;
		$value['type']=$type;
		$value['status']=$status;
		$this->assign('value',$value);
		$this->assign('data',$data);
		$this->display('lists');
	}

	//消息详情
	function moremany(){
		$usermodel = D("Common/User");
		$userId = I('param.user_id', 0);
		$id['id']=$userId;
		$result=$this->user_merssage_model->where($id)->find();
		$this->assign("data", $result);
		$this->display('detail');
	}

	//添加消息页面
	function add(){
		if (IS_POST) {
			$parent_id = I('post.pro_id');
			$region =$this->area_city->where(array('parent_code' => $parent_id))->select();
			$opt = '<option>-请选择--</option>';
			foreach($region as $key=>$val){
				$opt .= "<option value='{$val['city_code']}'>{$val['city_name']}</option>";
			 }
			 echo json_encode($opt);
        } else {
			$type_arr = array(
				0=>array("id"=>3,"name"=>"专题"),
				1=>array("id"=>4,"name"=>"视频"),
				2=>array("id"=>5,"name"=>"图集"),
				3=>array("id"=>6,"name"=>"文本"),
				4=>array("id"=>7,"name"=>"图文"),
				5=>array("id"=>8,"name"=>"直播")
			);
			$area_model = M('vrnews1.area',null);
			$area_list = $area_model->where(array('disable_status'=>0, 'establish_status'=>1))->select();
			$area['city_level'] = 1;
			$region = $this->area_city->where($area)->select();
			$this->assign('region',$region);
			$this->assign('type_arr',$type_arr);
			$this->assign('area_list',$area_list);
			$this->display();
		}
	}

	//消息添加
	function addok(){
			//接受数据
//			$object=I('post.object');
//			$sex=I('post.sex');
//			$area=I('post.town');
//			$pro=I('post.pro');
			$city=I('post.city');
			$city_arr = explode('_', $city);
			$content   = I('post.content');
			$url       = I('post.url');
			$title     = I('post.title');
			$news_type = I('post.news_type');
//			$is_time=I('post.is_time');
//			$start_time=I('post.start_time');
//			$end_time=I('post.end_time');
		
//			if($is_time==0){
				$data=array(
					'type'=>0,
//					'sex'=>$sex,
//					'area_code'=>$area,
//					'provice_code'=>$pro,
//					'city_code'=>$city,
					'content'=>'',//$content,
					'title'=>$title,
					'send_date'=>date('Y-m-d H:i:s'),
					'create_date'=>date('Y-m-d H:i:s'),
					'url'=>$url,
					'news_type'=>$news_type,
//					'is_time'=>$is_time,
					'send_status'=>'1',
					'status'=>'1', //发送中
				);
			if($city_arr[0] != 'all'){
				$data['city_id']  = $city_arr[0];
				$data['cityName'] = $city_arr[1];
			}
//			}elseif($is_time==1){
//				$data=array(
//				'type'=>$object,
//				'sex'=>$sex,
//				'area_code'=>$area,
//				'provice_code'=>$pro,
//				'city_code'=>$city,
//				'start_date'=>$start_time,
//				'end_date'=>$end_time,
//				'title'=>$title,
//				'send_date'=>date('Y-m-d H:i:s',time()),
//				'content'=>$content,
//				'url'=>$url,
//				'is_time'=>$is_time,
//				'send_status'=>'1'
//				);
//			}
			$insert=$this->user_merssage_model->data($data)->add();
			if(empty($insert)){
				$this->display('add');
			}else{
				//记录redis

				$this->success("添加成功", U("Admin/Movement/lists"));
			}
	}

	//消息重新操作页面
	function update(){
		if (IS_POST) {
			$parent_id = I('post.pro_id');
			$region =$this->area_city->where(array('parent_code' => $parent_id))->select();
			$opt = '<option>-请选择--</option>';
			foreach($region as $key=>$val){
				$opt .= "<option value='{$val['city_code']}'>{$val['city_name']}</option>";
			 }
			 echo json_encode($opt);
        } else {
			$id['id']=I('get.id');
			$value=$this->user_merssage_model->where($id)->find();
			$area['city_level'] = 1;
            $region = $this->area_city->where($area)->select();
			$type_arr = array(
				0=>array("id"=>3,"name"=>"专题"),
				1=>array("id"=>4,"name"=>"视频"),
				2=>array("id"=>5,"name"=>"图集"),
				3=>array("id"=>6,"name"=>"文本"),
				4=>array("id"=>7,"name"=>"图文"),
				5=>array("id"=>8,"name"=>"直播")
			);
			$area_model = M('vrnews1.area',null);
			$area_list = $area_model->where(array('disable_status'=>0, 'establish_status'=>1))->select();
			$this->assign('area_list',$area_list);
			$this->assign('type_arr',$type_arr);
			$this->assign('value',$value);
            $this->assign('region',$region);
			$this->assign('id',$id['id']);
			$this->display();
		}
	}

	//消息重新操作
	function updateok(){
		//接受数据
			$id=I('post.id');
//			$object=I('post.object');
//			$sex=I('post.sex');
//			$area=I('post.town');
//			$pro=I('post.pro');
			$city=I('post.city');
			$city_arr = explode('_', $city);
			$title     = I('post.title');
			$content   = I('post.content');
			$url       = I('post.url');
			$news_type = I('post.news_type');
//			$is_time=I('post.is_time');
//			$start_time=I('post.start_time');
//			$end_time=I('post.end_time');
			$data=array(
//				'type'=>$object,
//				'sex'=>$sex,
//				'area_code'=>$area,
//				'provice_code'=>$pro,
//				'city_code'=>$city,
//				'start_time'=>$start_time,
//				'end_time'=>$end_time,
				'content'=>$content,
				'title'=>$title,
				'news_type'=>$news_type,
				'url'=>$url,
				'status'=>'1',
//				'is_time'=>$is_time
			);
			if($city_arr[0] != 'all'){
				$data['city_id']  = $city_arr[0];
				$data['cityName'] = $city_arr[1];
			}
			$insert=$this->user_merssage_model->where(array('id'=>$id))->data($data)->save();
			if(empty($insert)){
				redirect('Movement/add');
			}else{
				redirect('Movement/lists');
			}
	}

	//通知列表
	function lists1()
	{
		$p=$this->user_merssage_model->getpage1(2);
		$list=$this->user_merssage_model->where(array('send_status'=>'0'))->select();
		$this->list=$list;
		$page=$p->show();
		$this->assign('data',$list);
		$this->assign('page',$page);
		$this->display();
	}

	//通知搜索
	function serachlists1(){
		if(!empty($_POST)){
			$title=I('param.title', 0);    //标题
			$time=I('param.time', 0);        //时间
			$status=I('param.status', 0);		 //状态
			$data=$this->user_merssage_model->serach1($title,$time,$status);
			$value['title']=$title;
			$value['time']=$time;
			$value['status']=$status;
			$this->assign('value',$value);
			$this->assign('data',$data);
			$this->display('lists1');
		}
	}

	//添加通知页面
	function add1(){
		if (IS_POST) {
			$parent_id = I('post.pro_id');
			$region =$this->area_city->where(array('parent_code' => $parent_id))->select();
			$opt = '<option>-请选择--</option>';
			foreach($region as $key=>$val){
				$opt .= "<option value='{$val['city_code']}'>{$val['city_name']}</option>";
			 }
			 echo json_encode($opt);
        } else {
			$area['city_level'] = 1;
            $region = $this->area_city->where($area)->select();
            $this->assign('region',$region);
			$this->display();
		}
	}

	//通知添加
	function addok1(){
			//接受数据
			$content=I('post.content');
			$url=I('post.url');
			$title=I('post.title');
			$is_time=I('post.is_time');
			$start_time=I('post.start_time');
			$end_time=I('post.end_time');
			if($is_time==0){
				$data=array(
				'content'=>$content,
				'title'=>$title,
				'send_date'=>date('Y-m-d H:i:s',time()),
				'url'=>$url,
				'is_time'=>$is_time,
				'send_status'=>'0'
				);
			}elseif($is_time==1){
				$data=array(
				'start_date'=>$start_time,
				'end_date'=>$end_time,
				'title'=>$title,
				'send_date'=>date('Y-m-d H:i:s',time()),
				'content'=>$content,
				'url'=>$url,
				'is_time'=>$is_time,
				'send_status'=>'0'
				);
			}
			$insert=$this->user_merssage_model->data($data)->add();
			if(empty($insert)){
				redirect('Movement/add1');
			}else{
				redirect('Movement/lists1');
			}
	}
	

	//通知详情
	function moremany1(){
		$usermodel = D("Common/User");
		$userId = I('param.user_id', 0);
		$id['id']=$userId;
		$result=$this->user_merssage_model->where($id)->find();
		$this->assign("data", $result);
		$this->display('detail1');
	}


	//通知重新操作页面
	function update1(){
		if (IS_POST) {
			$parent_id = I('post.pro_id');
			$region =$this->area_city->where(array('parent_code' => $parent_id))->select();
			$opt = '<option>-请选择--</option>';
			foreach($region as $key=>$val){
				$opt .= "<option value='{$val['city_code']}'>{$val['city_name']}</option>";
			 }
			 echo json_encode($opt);
        } else {
			$id['id']=I('get.id');
			$value=$this->user_merssage_model->where($id)->find();
			$area['city_level'] = 1;
            $region = $this->area_city->where($area)->select();
			$this->assign('value',$value);
            $this->assign('region',$region);
			$this->display();
		}
	}


	//通知重新操作
	function updateok1(){
		//接受数据
			$id=I('post.id','');
			$title=I('post.title','');
			$content=I('post.content');
			$url=I('post.url');
			$data=array(
				'title'=>$title,
				'content'=>$content,
				'url'=>$url,
				'is_time'=>$is_time
			);
			$insert=$this->user_merssage_model->where(array('id'=>$id))->data($data)->save();
			if(empty($insert)){
				redirect('Movement/add1');
			}else{
				redirect('Movement/lists1');
			}
	}

	/*
     * 过滤敏感词
     */
	public function filter_words(){
		$qian= array(" ","　","\t","\n","\r");
		$hou = array("","","","","");
		$content = str_replace($qian,$hou,I('content'));
		$title   = str_replace($qian,$hou,I('title'));
		$name    = str_replace($qian,$hou,I('name'));
		$option_name  = str_replace($qian,$hou,I('option_name'));
		$introduction = str_replace($qian,$hou,I('introduction'));
		$live_man_cate_id  = str_replace($qian,$hou,I('live_man_cate_id'));
		$live_man_alias_id = str_replace($qian,$hou,I('live_man_alias_id'));
		$zhuban_name   = str_replace($qian,$hou,I('zhuban_name'));
		$chengban_name = str_replace($qian,$hou,I('chengban_name'));
		$vote  = str_replace($qian,$hou,I('vote'));
		$group = str_replace($qian,$hou,I('group'));
		$vote_class_arr = str_replace($qian,$hou,I('vote_class_arr'));
		$data = str_replace($qian,$hou,I('data'));
//		$param = $_REQUEST;
//		$content = array_values($param);
		$sensitive = '';
		$redis     = new Redis();
		$word_list = $redis->get(C("environment").'_sensitive_word');
		if(!$word_list && count($word_list) < 1){
			exit($sensitive);
		}

		if(!empty($data) || count($data) > 0){
			if(is_array($data)){
				for($i=0;$i<count($data);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($data[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($data, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($zhuban_name)){
			if(is_array($zhuban_name)){
				for($i=0;$i<count($zhuban_name);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($zhuban_name[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($zhuban_name, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($chengban_name)){
			if(is_array($chengban_name)){
				for($i=0;$i<count($chengban_name);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($chengban_name[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($chengban_name, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($vote)){
			if(is_array($vote)){
				for($i=0;$i<count($vote);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($vote[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($vote, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($group)){
			if(is_array($group)){
				for($i=0;$i<count($group);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($group[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($group, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($vote_class_arr)){
			if(is_array($vote_class_arr)){
				for($i=0;$i<count($vote_class_arr);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($vote_class_arr[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($vote_class_arr, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($content)){
			foreach ($word_list as $key=>$value){
				if(strstr($content, $value['words'])){
					$sensitive .= $value['words'].',';
				}
			}
		}
		if(!empty($title)){
			foreach ($word_list as $key=>$value){
				if(strstr($title, $value['words'])){
					$sensitive .= $value['words'].',';
				}
			}
		}
		if(!empty($name)){
			foreach ($word_list as $key=>$value){
				if(strstr($name, $value['words'])){
					$sensitive .= $value['words'].',';
				}
			}
		}
		if(!empty($option_name)){
			if(is_array($option_name)){
				for($i=0;$i<count($option_name);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($option_name[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($option_name, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($introduction)){
			if(is_array($introduction)){
				for($i=0;$i<count($introduction);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($introduction[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($introduction, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($live_man_alias_id)){
			if(is_array($live_man_alias_id)){
				for($i=0;$i<count($live_man_alias_id);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($live_man_alias_id[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($live_man_alias_id, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($live_man_cate_id)){
			if(is_array($live_man_cate_id)){
				for($i=0;$i<count($live_man_cate_id);$i++){
					foreach ($word_list as $key=>$value){
						if(strstr($live_man_cate_id[$i], $value['words'])){
							$sensitive .= $value['words'].',';
						}
					}
				}
			}else{
				foreach ($word_list as $key=>$value){
					if(strstr($live_man_cate_id, $value['words'])){
						$sensitive .= $value['words'].',';
					}
				}
			}
		}
		if(!empty($sensitive)){
			$news_arr = explode(',', $sensitive);
			$news_arrs = array_unique($news_arr);
			$sensitives = implode(',', $news_arrs);
			exit($sensitives);
		}else{
			exit($sensitive);
		}

	}
}